<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="list" :default-sort="{prop: 'timeOfEntry', order: 'ascending'}">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" sortable />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="{row}">
              {{ formatEmployment(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable />
          <!-- <el-table-column label="账户状态" prop="" /> -->
          <el-table-column label="操作" width="280">
            <template #default="{row}">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="hDdl(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="page"
            :page-sizes="[2, 5,10, 20]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          /></el-row>
      </el-card>
      <el-Dialog title="新增员工" :visible.sync="showDialog" @close="hClose">
        <empDialog ref="empDialog" @success="hSuccess" @close="showDialog=false" />
      </el-Dialog>
    </div>
  </div>
</template>
<script>
import PageTools from '@/components/PageTools'
import { getEmployeeList, delEmployee } from '@/api/employees'
import EmployeeEnum from '@/constant/employees'
import empDialog from '@/views/employees/empDialog.vue'
const hireType = {}
EmployeeEnum.hireType.forEach(item => {
  hireType[item.id] = item.value
})
export default {
  components: {
    PageTools,
    empDialog
  },
  data() {
    return {
      showDialog: false, // 控制增加数据的显示和隐藏
      list: [], // 员工数据
      page: 1, // 页码值
      size: 5, // 每页显示的条数
      total: 0 // 总数据
    }
  },
  created() {
    this.loadEmployeeList()
  },
  methods: {
    hDdl(id) {
      this.$confirm('您确定要删除该员工吗???').then(async() => {
        const res = await delEmployee(id)
        console.log(res)
        if (this.list.length === 1) {
          this.page--
        }
        if (this.page <= 0) {
          this.page = 1
        }
        this.loadEmployeeList()
      }).catch(err => err)
    },
    hClose() {
      this.$refs.empDialog.resetForm()
    },
    // 重新渲染页面
    hSuccess() {
      this.total++
      this.page = Math.ceil(this.total / this.size)
      this.loadEmployeeList()
      this.showDialog = false
    },
    handleSizeChange(val) {
      this.page = 1
      this.size = val
      this.loadEmployeeList()
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.page = val
      this.loadEmployeeList()
      console.log(`当前页: ${val}`)
    },
    // 获取员工信息
    async loadEmployeeList() {
      try {
        const res = await getEmployeeList(this.page, this.size)
        res.data.rows
        this.list = res.data.rows
        this.total = res.data.total
        // console.log('getEmployeeList', res)
        // 保存数据
      } catch (err) {
        console.log('getEmployeeList', err)
      }
    },
    formatEmployment(data) {
      return hireType[data] || '未知'
    }
  }
}

</script>
